<?php
/**
 * Created by jinqiang on 2024/9/11 14:17.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Soa;


use App\Http\Models\Mysql\SsyFlowWfRunTable;
use App\Http\Models\Mysql\SsyWorkingProcedureAmountTable;
use App\Http\Repository\ApprovalRepository;

class WorkingService
{
    public function search($param)
    {
        $conn = (new SsyWorkingProcedureAmountTable());
        if($param['type_no'] ?? ""){
            $conn = $conn->where('type_no', 'like', $param['type_no']."%");
        }
        if($param['horse_embroidery_id'] ?? ""){
            $conn = $conn->where('horse_embroidery_id', $param['horse_embroidery_id']);
        }
        if(isset($param['k_status']) && $param['k_status'] != ''){
            $conn = $conn->where('k_status', (Int)$param['k_status']);
        }
        if(isset($param['status']) && $param['status'] != ''){
            $conn = $conn->where('status', (Int)$param['status']);
        }
        $res = $conn->orderBy('createtime', 'desc')->paginate((Int)$param['limit'])->toArray();
        $ids = array_column($res['data'], 'id');
        $runIdMap = [];
        if(!empty($ids)){
            $runList = (new SsyFlowWfRunTable())->where('c_type', 5)->whereIn('c_id', $ids)->get(['id', 'c_id'])->toArray();
            $runIdMap = array_column($runList, 'id', 'c_id');
        }
        foreach ($res['data'] as &$item){
            $item['run_id'] = $runIdMap[$item['id']] ?? "";
        }
        unset($item);
        return ['data' => $res['data'], 'total' => $res['total']];
    }

    public function approvalProcess($param)
    {
        //获取审批流程
        $flow = ApprovalRepository::getFlow((Int)$param['run_id']);
        //获取审批数据
        $workingObj = (new SsyWorkingProcedureAmountTable())->where('id', (Int)$param['id'])->first()->toArray();
        $workingObj['base_list'] = [
            [
                'type_no' => $workingObj['type_no'],
                'working_number' => $workingObj['working_number']
            ]
        ];
        $workingObj['price_list'] = [
            [
                'price' => $workingObj['price'],
                'num' => $workingObj['num'],
                'amount' => $workingObj['amount']
            ]
        ];
        return ['flow' => $flow, 'data' => $workingObj];
    }
}
